declare @TempIds table (id varchar(max),idEsp varchar(15))
declare @query nvarchar(max), @Esp varchar(15), @id varchar(max), @esplen varchar(max)

declare qIds Cursor for
		select left(LastName,1)+'-'+ left(FirstName,1) + '-' +
	
		case 
		 when  BirthDate is null  then 
		     

			  cast(DATEPART(year,BirthDate)as varchar(max)) 
  
			 
		 else
		   
		       cast(DATEPART(year,CURRENT_TIMESTAMP)as varchar(max))
		end
		 , cast(EmployeeID as varchar(max)) from Employees
		
open qIds

Fetch next from qIds into @Esplen , @id

while @@FETCH_STATUS = 0

begin
while  len(@Esplen) < 15
begin

	if len(@esplen + '0' + '-'+@id) = 15
	begin
	set @esplen = @esplen + '0' + @id
	end
	else
	begin
	set @esplen = @esplen + '0'
	end
end

insert into @TempIds values(@id,@esplen) 

fetch next from qIds into @esplen , @id

end
close qIds
Deallocate qIds 

select * from Employees
join @TempIds as t on Employees.EmployeeID = t.id